package com.ming.mallchat.common.user.service.adpater;

import com.ming.mallchat.common.user.domain.vo.resp.friend.ChatMemberResp;
import com.ming.mallchat.common.user.pojo.User;
import com.ming.mallchat.common.user.pojo.UserFriend;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class MemberAdapter {

    public static List<ChatMemberResp> buildMember(List<UserFriend> friendUser, List<User> userList) {
        Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, user -> user));
        return friendUser.stream()
                .map(friendList->{
                    ChatMemberResp resp = new ChatMemberResp();
                    resp.setUid(friendList.getFriendUid());
                    User user = userMap.get(friendList.getFriendUid());
                    if (Objects.nonNull(user)) {
                        resp.setName(user.getName());
                        resp.setAvatar(user.getAvatar());
                        resp.setActiveStatus(user.getActiveStatus());
                    }
                    return resp;
                }).collect(Collectors.toList());
    }
}
